package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.MaterialType.MaterialTypeService;
import com.bdqn.entity.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/MaterialType")
public class MaterialTypeController {
    @Autowired
    private MaterialTypeService materialTypeService;
    @RequestMapping("/product_category_maintenance.html")
    @ResponseBody
    public Layui<MaterialType> selectMaterialType(String materialNumber,String  nameOfGoods,Integer page,Integer limit){
        //判断当前页是否为空如果为空那就为第一页
        if(page==null||page.equals("")){
            page=1;
        }
        //获得sql里面的
        if(limit==null||limit.equals("")){
            limit=10;
        }
        page= (page - 1) * limit;
           //获取查询方法 条件和分页
        List<MaterialType> materialTypes = materialTypeService.selectMaterialType(materialNumber, nameOfGoods, page, limit);
        Layui<MaterialType> materialTypeLayui = new Layui<>();
        materialTypeLayui.setCode(0);
        materialTypeLayui.setData(materialTypes);
        System.out.println(materialTypes.toString());
        //获取总条数
        int  count=materialTypeService.getMaterialTypeCount(materialNumber,nameOfGoods);
        materialTypeLayui.setCount(count);
        //总数量，查
        return materialTypeLayui;
    }
    @RequestMapping("/DeleteMaterialType.html")
    @ResponseBody
    private int deleteMaterialType(Integer id){
      return   materialTypeService.deleteMaterialType(id);
    }


    /**
     * 添加
     * @param materialType
     * @return
     */
    @RequestMapping("/addMaterialType.html")
    @ResponseBody
    private int addMaterialType(MaterialType materialType){
        return  materialTypeService.insertMaterialType(materialType);
    }

    /**
     * 修改第一步 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/product_class_update.html")
//    @ResponseBody
    private  String  updateMaterialType(Integer id, Model model){
        MaterialType materialType = materialTypeService.selectMaterialTypeById(id);
        model.addAttribute("m",materialType);
//        String aa=materialType.toString();
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa"+materialType.toString());
        return  "/supplier/product_class_update.html";
}

    /**
     * 第二部修改
     * @param materialType
     * @return
     */
    @RequestMapping("/updateMaterialTypeOK.html")
    @ResponseBody
    private  int updateMaterialTypeOK(MaterialType materialType){
        int de= materialTypeService.updateMaterialType(materialType);
        return  de;
    }

}
